package com.apes.autoparts.supplier.todo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.query.ApesDao;
import org.springframework.stereotype.Component;

/**
 * Created by lishikun on 2021/4/10 15:06
 */
@Component
public class SupplierPlatformTodoDao extends ApesDao {


    public String findTodo(JSONObject param){

        String sSQL = "SELECT A.ID, A.CREATOR_ID,\n" +
                "       A.INNER_BUSINESS_TYPE, A.UPDATER_ID,  A.BATCH, A.BRAND_NAME,\n" +
                "       A.CONFIRM_DATE, A.OLD_BRAND_NAME,A.OLD_ORIGIN_PLACE, A.OLD_PART_CODE,\n" +
                "       A.OLD_PART_CODE_NAME, A.OLD_UNIT, A.ORIGIN_PLACE, A.PART_CODE, A.PART_CODE_NAME,\n" +
                "       A.REMARK, A.RESOLVE_DATE, A.SOURCE, A.STATE, A.STOCK_QTY, A.UNIT, A.UNTAX_PRICE,\n" +
                "       A.SCENE_ID, A.CONFIRM_ID, A.PROPERTY, A.RESOLVE_ID, A.SUPPLIER_PLATFORM_ITEM_ID\n" +
                "  FROM SUPPLIER_PLATFORM_TODO A , supplier_platform_todo_parse C,\n" +
                "  SUPPLIER_PLATFORM_ITEM B\n" +
                "  WHERE 1 = 1\n" ;
                if ( param.getString("state") != null)
                    sSQL += "  AND   A.STATE = :state\n" ;
                if ( param.getString("propertyId") != null)
                    sSQL += "  AND A.property = :propertyId\n" ;
                if ( param.getString("id") != null)
                    sSQL += "  AND A.ID = :id\n" ;
                if ( param.getString("supplierPlatformItemSupplierPlatformId") != null)
                    sSQL += "  AND B.SUPPLIER_PLATFORM_ID = :supplierPlatformItemSupplierPlatformId\n" ;
                if ( param.getString("supplierPlatformItemSupplierId") != null)
                    sSQL += "  AND B.supplier_id = :supplierPlatformItemSupplierId\n" ;
                if ( param.getString("batch") != null)
                    sSQL += "  AND A.batch = :batch\n" ;
                if ( param.getString("oldPartCode") != null)
                    sSQL += "  AND A.old_part_code = :oldPartCode\n" ;
                if ( param.getString("oldOriginPlace") != null)
                    sSQL += "  AND A.old_origin_place = :oldOriginPlace\n" ;
                if ( param.getString("contrastCategoryId") != null)
                    sSQL += "  AND C.contrast_category_id = :contrastCategoryId\n" ;
                if ( param.getString("source") != null)
                    sSQL += "  AND A.source = :source\n" ;
                if ( param.getString("contrastBrandId") != null)
                    sSQL += "  AND C.contrast_brand_id = :contrastBrandId\n" ;
                if ( param.getString("maxID") != null)
                    sSQL += "  AND A.id > :maxID\n" ;
                sSQL +=    "  and a.id = c.todo_id\n" +
                "  and a.supplier_platform_item_id = b.id " +
                " ORDER BY ID asc";

        return sSQL;
    }

    public JSONArray findTodoPage(JSONObject param) {

        String sSQL = "SELECT G.*\n" +
                "  FROM (SELECT G.*, ROWNUM RN, CEIL(TOTAL / :SIZE) PAGE\n" +
                "           FROM (SELECT MAX(ROWNUM) OVER() TOTAL, G.*\n" +
                "                    FROM ("+ findTodo(param) +"\n" +
                "                   ) G\n" +
                "          WHERE ROWNUM <= DECODE(0, :PAGE, :SIZE, (:PAGE + 1) * :SIZE)) G) G\n" +
                " WHERE RN > (:PAGE) * :SIZE" ;
        return toJSONArray(find(sSQL, param));
    }

    public JSONArray findTodoTotal(JSONObject param) {
        String sSQL = "select count(*) TOTAL from (" + findTodo(param) + " ) a" ;

        return toJSONArray(find(sSQL, param));
    }
}
